package net.youdi.gateway.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author:scl
 * @date:2019-10-14 15:11
 * @description
 */
@RestController
@RequestMapping("apps")
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*", methods = {RequestMethod.GET})
public class AppServiceController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private ApplicationContext applicationContext;

    private List<String> removeServices = Arrays.asList("config-server");

    @GetMapping("list")
    public Object list(){
        Map<String, Object> result = new HashMap<>();
        result.put("code",0);
        List<String> services = discoveryClient.getServices();
        services = services.stream().filter(s->!removeServices.contains(s)&&!applicationContext.getId().contains(s)).collect(Collectors.toList());
        result.put("data",services);
        return result;
    }
}
